<script setup>
import { getDetailAPI } from '@/api/Sales/detail'
import { ref } from 'vue'
import { useRoute } from 'vue-router'
import GoodsSku from '@/components/Sales/GoodsSku.vue'
import { useCartStore } from '@/store/Sales'
import { ElMessage } from 'element-plus'
// 获取商品详情
const good = ref({})
const num = ref(1)
const route = useRoute()
const getGoods = async () => {
  const res = await getDetailAPI(route.params.id)
  good.value = res.result
}

getGoods()

// 加入购物车逻辑
// 1.选择商品规格
const skuObj = ref({})
const skuChange = (sku) => {
  skuObj.value = sku
}
// 2.输入数量
// const num = ref(1)
const countChange = (count) => {
  num.value = count
}
// 3.点击加入购物车
const cartStore = useCartStore()
const add = () => {
  if (skuObj.value.skuId) {
    // 规格选择完成，触发cartStore的action
    cartStore.addCart({
      id: good.value.id,
      name: good.value.name,
      picture: good.value.mainPictures,
      price: good.value.price,
      count: num.value,
      skuId: skuObj.value.skuId,
      attrsText: skuObj.value.specsText,
      selected: true
    })
    ElMessage.success('加入购物车成功')
  } else {
    // 规格没有选择，提示用户
    ElMessage.warning('请选择完整规格')
  }
}
</script>
<!-- 错误原因：goods一开始{}  {}.categories -> undefined  -> undefined[1]
1. 可选链的语法?. 
2. v-if手动控制渲染时机 保证只有数据存在才渲染-->
<template>
  <div class="xtx-goods-page">
    <div class="container" v-if="good.details">
      <div class="bread-container">
        <el-breadcrumb separator=">">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item
            :to="{ path: `/category/${good.categories?.[1].id}` }"
            >{{ good.categories?.[1].name }}
          </el-breadcrumb-item>
          <el-breadcrumb-item
            :to="{ path: `/category/sub/${good.categories?.[0].id}` }"
            >{{ good.categories?.[0].name }}
          </el-breadcrumb-item>
          <el-breadcrumb-item>{{ good.name }}</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <!-- 商品信息 -->
      <div class="info-container">
        <div>
          <div class="goods-info">
            <div class="media">
              <!-- 图片预览区 -->
              <goods-image :imageList="good.mainPictures"></goods-image>
              <!-- 统计数量 -->
              <ul class="goods-sales">
                <li>
                  <p>销量人气</p>
                  <p>{{ good.salesCount }}+</p>
                  <p><i class="iconfont icon-task-filling"></i>销量人气</p>
                </li>
                <li>
                  <p>商品评价</p>
                  <p>{{ good.commentCount }}+</p>
                  <p><i class="iconfont icon-comment-filling"></i>查看评价</p>
                </li>
                <li>
                  <p>收藏人气</p>
                  <p>{{ good.collectCount }}+</p>
                  <p><i class="iconfont icon-favorite-filling"></i>收藏商品</p>
                </li>
              </ul>
            </div>
            <div class="spec">
              <!-- 商品信息区 -->
              <p class="g-name">{{ good.name }}</p>
              <p class="g-desc">{{ good.description }}</p>
              <p class="g-price">
                <span>{{ good.price }}</span>
                <span>{{ good.oldPrice }}</span>
              </p>
              <div class="g-service">
                <dl>
                  <dt>促销</dt>
                  <dd>好物放送，领券购买直接抵现</dd>
                </dl>
                <dl>
                  <dt>服务</dt>
                  <dd>
                    <span>无忧退货</span>
                    <span>快速退款</span>
                    <span>免费包邮</span>
                    <a href="javascript:;">了解详情</a>
                  </dd>
                </dl>
              </div>
              <!-- sku组件 -->
              <GoodsSku :goods="good" @change="skuChange"></GoodsSku>
              <!-- 数据组件 -->
              <el-input-number v-model="num" :min="1" @change="countChange" />
              <!-- 按钮组件 -->
              <div>
                <el-button size="large" class="btn" @click="add">
                  加入购物车
                </el-button>
              </div>
            </div>
          </div>
          <div class="goods-footer">
            <div class="goods-article">
              <!-- 商品详情 -->
              <div class="goods-tabs">
                <nav>
                  <a>商品属性</a>
                </nav>
                <div class="goods-detail">
                  <!-- 属性 -->
                  <ul class="attrs">
                    <li
                      v-for="item in good.details.properties"
                      :key="item.value"
                    >
                      <span class="dt">{{ item.name }}</span>
                      <span class="dd">{{ item.value }}</span>
                    </li>
                  </ul>
                </div>
                <nav>
                  <a>商品详情</a>
                </nav>
                <div class="goods-detail">
                  <!-- 图片 -->
                  <view v-for="item in good.details.pictures">
                    <img v-img-lazy="item" alt="" />
                  </view>
                </div>
              </div>
            </div>
            <div class="goods-aside">
              <!-- 24小时热榜 -->
              <!-- <goods-hot :type="1"></goods-hot> -->
              <!-- 周热榜 -->
              <goods-hot :type="2"></goods-hot>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.xtx-goods-page {
  .goods-info {
    min-height: 600px;
    background: #fff;
    display: flex;

    .media {
      width: 580px;
      height: 600px;
      padding: 30px 50px;
    }

    .spec {
      flex: 1;
      padding: 30px 30px 30px 0;
    }
  }

  .goods-footer {
    display: flex;
    margin-top: 20px;

    .goods-article {
      width: 940px;
      margin-right: 20px;
    }

    .goods-aside {
      width: 280px;
      min-height: 500px;
    }
  }

  .goods-tabs {
    min-height: 600px;
    background: #fff;
  }

  .goods-warn {
    min-height: 600px;
    background: #fff;
    margin-top: 20px;
  }

  .number-box {
    display: flex;
    align-items: center;

    .label {
      width: 60px;
      color: #999;
      padding-left: 10px;
    }
  }

  .g-name {
    font-size: 22px;
  }

  .g-desc {
    color: #999;
    margin-top: 10px;
  }

  .g-price {
    margin-top: 10px;

    span {
      &::before {
        content: '¥';
        font-size: 14px;
      }

      &:first-child {
        color: #cf4444;
        margin-right: 10px;
        font-size: 22px;
      }

      &:last-child {
        color: #999;
        text-decoration: line-through;
        font-size: 16px;
      }
    }
  }

  .g-service {
    background: #f5f5f5;
    width: 500px;
    padding: 20px 10px 0 10px;
    margin-top: 10px;

    dl {
      padding-bottom: 20px;
      display: flex;
      align-items: center;

      dt {
        width: 50px;
        color: #999;
      }

      dd {
        color: #666;

        &:last-child {
          span {
            margin-right: 10px;

            &::before {
              content: '•';
              color: #27ba9b;
              margin-right: 2px;
            }
          }

          a {
            color: #27ba9b;
          }
        }
      }
    }
  }

  .goods-sales {
    display: flex;
    width: 400px;
    align-items: center;
    text-align: center;
    height: 140px;

    li {
      flex: 1;
      position: relative;

      ~ li::after {
        position: absolute;
        top: 10px;
        left: 0;
        height: 60px;
        border-left: 1px solid #e4e4e4;
        content: '';
      }

      p {
        &:first-child {
          color: #999;
        }

        &:nth-child(2) {
          color: #cf4444;
          margin-top: 10px;
        }

        &:last-child {
          color: #666;
          margin-top: 10px;

          i {
            color: #27ba9b;
            font-size: 14px;
            margin-right: 2px;
          }

          &:hover {
            color: #27ba9b;
            cursor: pointer;
          }
        }
      }
    }
  }
}

.goods-tabs {
  min-height: 600px;
  background: #fff;

  nav {
    height: 70px;
    line-height: 70px;
    display: flex;
    border-bottom: 1px solid #f5f5f5;

    a {
      padding: 0 40px;
      font-size: 18px;
      position: relative;

      > span {
        color: #cf4444;
        font-size: 16px;
        margin-left: 10px;
      }
    }
  }
}

.goods-detail {
  padding: 40px;

  .attrs {
    display: flex;
    flex-wrap: wrap;
    margin-bottom: 30px;

    li {
      display: flex;
      margin-bottom: 10px;
      width: 50%;

      .dt {
        width: 100px;
        color: #999;
      }

      .dd {
        flex: 1;
        color: #666;
      }
    }
  }

  > img {
    width: 100%;
  }
}

.btn {
  margin-top: 20px;
}

.bread-container {
  padding: 25px 0;
}
</style>
